﻿
var LoginView = Base.extend({
    initialize: function initialize(host) {
        this.host = host;
        this.$loginDiv = null;
        this.$loginButton = null;
        this.$emailInput = null;
        this.$title = null;
        this.$passwordInput = null;
        this.inputs = [];
        this.viewId = "login";
        this.isActive = false;
        this.paintInterval = null;
        this.frameRate = 100;
        this.bounce = 0;
        this.bounceOffset = 1;
        this.color = 255;
        this.colorOffset = -10;
    },
    setup: function setup() {
        var self = this;

        this.$loginDiv = $("#loginDiv");
        var d = this.$loginDiv;
        d.offset({ top: (parseInt($(window).height() - 230) / 2), left: parseInt(($(window).width() - 430) / 2) });

        this.$title = d.find(".login-title");
        this.$loginButton = d.find("#loginBtn");
        this.$emailInput = d.find("#emailTxt");
        this.$passwordInput = d.find("#passwordPwd");
        this.$loginButton.click(function (e) {
            self.host.notify("login", { email: self.$emailInput.val(), password: self.$passwordInput.val(), targetView: "fourkill" });
        });

        d.show();

        /*for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].isAvailable()) {
                this.inputs[k].enable();
            }
        }*/
        this.paintInterval = setInterval((function (self) { return function () { self.paint(); } })(this), this.frameRate);

        this.isActive = true;
    },
    teardown: function teardown() {
        /*for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].enabled) {
                this.inputs[k].disable();
            }
        }*/
        if (this.paintInterval != null) {
            clearInterval(this.paintInterval);
            this.paintInterval = null;
        }
        this.$loginDiv.hide();
        this.isActive = false;
    },
    pause: function pause() {
    },
    resume: function resume() {
    },
    resize: function resize() {
        if (this.isActive) {
            var d = this.$loginDiv;
            d.offset({ top: (parseInt($(window).height() - 230) / 2), left: parseInt(($(window).width() - 430) / 2) });
        }
    },
    paint: function paint() {
        this.color += this.colorOffset;
        this.bounce += this.bounceOffset;
        if (this.color <= 175 || this.color >= 255) this.colorOffset *= -1;
        if (this.bounce >= 12 || this.bounce <= 0) this.bounceOffset *= -1;
        var rand = util.random(50);
        if (rand == 25) {
            this.$title.css("font-weight", "normal");
            this.$title.css("color", "red");
        }
        else {
            this.$title.css("font-weight", "bold");
            this.$title.css("color", "rgb(" + this.color + "," + this.color + "," + this.color + ")");
        }
        this.$title.css("letter-spacing", "" + this.bounce + "px");
    }
});

var FourKillGameView = Base.extend({
    initialize: function initialize(host) {
        this.bgMusic = null;
        this.host = host;
        this.paintingContext = null;
        this.painters = [];
        this.objects = [];
        this.inputs = [];
        this.paintInterval = null;
        this.frameRate = 100;
        this.viewId = "fourkill";
        this.isActive = false;
        this.volume = 0.0;
    },
    setup: function setup() {
        this.bgMusic = $("#bgMusic").get(0);
        this.bgMusic.loop = "loop";
        this.bgMusic.play(0);
        this.bgMusic.volume = this.volume;
        var $backCanvas = $('<canvas/>', { 'class': 'radHuh', 'id': 'backCanvas' })
           .attr("width", $(window).width())
           .attr("height", $(window).height())
           .offset({ top: 0, left: 0 })
           .css("position", "absolute")
           .css("zIndex", "0");

        var $foreCanvas = $('<canvas/>', { 'class': 'radHuh', 'id': 'foreCanvas' })
           .attr("width", $(window).width())
           .attr("height", $(window).height())
           .offset({ top: 0, left: 0 })
           .css("position", "absolute")
           .css("zIndex", "1");

        $(document.body).append($backCanvas);
        $(document.body).append($foreCanvas);
        
        this.paintingContext = PaintingContext.newInstance();
        this.paintingContext.backLayer = $backCanvas.get(0).getContext("2d");
        this.paintingContext.frontLayer = $foreCanvas.get(0).getContext("2d");
        this.paintingContext.viewPort = ViewPort.newInstance(6, util._90, util._45);
        this.paintingContext.camera = Placement.newInstance();
        this.paintingContext.camera.w = 3.0;
        this.paintingContext.camera.h = 6.25;
        this.paintingContext.camera.x = 30.0;
        this.paintingContext.camera.y = 10.0;
        this.paintingContext.camera.setBearing(util._90);

        this.painters.push(Painters.Sky2);
        this.painters.push(Painters.Ground);
        this.inputs.push(InputHandlers.Tap);
        this.objects.push(MistObject.newInstance(1000, 0.2));

        /***********************************************************/
        var pc = this.paintingContext;
        /*Trees*/
        for (var j = 0; j < 500; j++) {
            var s = util.random(15, 70);
            var tree = TreeObject.newInstance();
            tree.w = s;
            tree.h = s;
            tree.z = 0;
            tree.x = util.random(pc.camera.x - 3600, pc.camera.x + 3600);
            tree.y = util.random(pc.camera.y - 3600, pc.camera.y + 3600);
            tree.setBearing(util.radians(util.random(180)));
            tree.update();
            this.objects.push(tree);
        }
        /*Birds*/
        for (var j = 0; j < 30; j++) {
            var bird = FlyingBirdObject.newInstance();
            bird.bearing = util.radians(util.random(0, 359));
            bird.h = util.random(2, 4);
            bird.w = bird.h;
            bird.x = util.random(pc.camera.x - 500, pc.camera.x + 500);
            bird.y = util.random(pc.camera.y - 500, pc.camera.y + 500);
            bird.color = "rgb(" + util.random(0, 255) + ", " + util.random(0, 255) + ", " + util.random(0, 255) + ")";
            bird.update();
            this.objects.push(bird);
        }
        /*Test Sprites*/
        for (var j = 0; j < 100; j++) {
            var dude = null;
            if ((j % 4) == 0) dude = TestDudeObject.newInstance(AdvancedSprites.Hero);
            else if ((j % 3) == 0) dude = TestDudeObject.newInstance(AdvancedSprites.Spencer);
            else if ((j % 2) == 0) dude = TestDudeObject.newInstance(AdvancedSprites.Hitler);
            else dude = TestDudeObject.newInstance(AdvancedSprites.Marine);

            dude.bearing = util.radians(util.random(0, 359));
            dude.h = 6;
            dude.w = 4;
            dude.x = util.random(pc.camera.x - 200, pc.camera.x + 200);
            dude.y = util.random(pc.camera.y - 200, pc.camera.y + 200);
            dude.color = "rgb(" + util.random(0, 255) + ", " + util.random(0, 255) + ", " + util.random(0, 255) + ")";
            dude.update();
            this.objects.push(dude);
        }
        /***********************************************************/


        for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].isAvailable()) {
                this.inputs[k].enable();
            }
        }

        this.paintInterval = setInterval((function (self) { return function () { self.paint(); } })(this), this.frameRate);
        this.isActive = true;
    },
    teardown: function teardown() {
        for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].enabled) {
                this.inputs[k].disable();
            }
        }
        $(this.paintingContext.backLayer.canvas).remove();
        $(this.paintingContext.frontLayer.canvas).remove();
        this.paintingContext = null;
        this.painters.length = 0;
        this.inputs.length = 0;
        this.objects.length = 0;
        if (this.paintInterval != null) {
            clearInterval(this.paintInterval);
            this.paintInterval = null;
        }
        this.isActive = false;
        this.bgMusic.pause();
    },
    pause: function pause() {
        this.bgMusic.pause();
        for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].enabled) {
                this.inputs[k].disable();
            }
        }
        if (this.paintInterval != null) {
            clearInterval(this.paintInterval);
            this.paintInterval = null;
        }
    },
    resume: function resume() {
        this.bgMusic.play();
        for (var k = 0; k < this.inputs.length; k++) {
            if (!this.inputs[k].enabled) {
                this.inputs[k].enable();
            }
        }
        if (this.paintInterval == null) {
            this.paintInterval = setInterval((function (self) { return function () { self.paint(); } })(this), this.frameRate);
        }
    },
    resize: function resize() {
        if (this.isActive) {
            var pc = this.paintingContext;
            pc.frontLayer.canvas.height = $(window).height();
            pc.frontLayer.canvas.width = $(window).width();
            pc.backLayer.canvas.height = $(window).height();
            pc.backLayer.canvas.width = $(window).width();
            this.paint();
        }
    },
    paint: function paint() {
        // bring music volume up
        if (this.bgMusic.volume < .5) this.bgMusic.volume += .005;

        var pc = this.paintingContext,
            fc = this.paintingContext.frontLayer,
            bc = this.paintingContext.backLayer;

        for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].enabled) {
                this.inputs[k].update(pc);
            }
        }
        fc.clearRect(0, 0, fc.canvas.width, fc.canvas.height);
        pc.viewPort.setPixelSize(fc.canvas.width, fc.canvas.height);
        pc.gameObject = null;
        
        for (var i = 0; i < this.painters.length; i++) {
            this.painters[i].paint(pc);
        }

        for (var i = 0; i < this.objects.length; i++) this.objects[i].updateLocation(pc);

        // Order painters by distance
        util.orderByDistance(this.objects);

        // Paint away!
        for (var i = 0; i < this.objects.length; i++) {
            var o = this.objects[i];
            o.tick();
            if (o.locationRelativeToCamera != null && o.locationRelativeToCamera.visible) {
                pc.gameObject = o;
                o.getPainter().paint(pc);
            }
        }
    }
});